<template>
  <div class="out-order" style="cursor:default;">
    <!-- 工具栏 -->
    <div class="head-container">
      <div class="head-container-tools">
        <template v-if="crud.props.searchToggle">
          <el-row>
            <el-col :span="6">
              <span>订单时间：</span>
              <el-date-picker
                v-model="query.orderTime"
                type="daterange"
                range-separator="至"
                size="small"
                class="order-filter-item charges-date-picker"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-col>
            <el-col :span="6">
              <span>支付时间：</span>
              <el-date-picker
                v-model="query.payTime"
                type="daterange"
                range-separator="至"
                size="small"
                class="order-filter-item charges-date-picker"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-col>
            <el-col :span="6">
              <el-input
                v-model="query.patientName"
                placeholder="患者姓名或手机号"
                class="order-filter-item"
                size="small"
              />
            </el-col>
            <el-col :span="6">
              <el-input
                v-model="query.docName"
                placeholder="医生姓名"
                class="order-filter-item"
                size="small"
              />
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-input
                v-model="query.orderNo"
                placeholder="订单编号"
                class="order-filter-item"
                size="small"
              />
            </el-col>
            <el-col :span="6">
              <el-select
                v-model="query.orderCondition"
                placeholder="处方状态"
                class="order-filter-item"
                clearable
              >
                <template v-for="item in statusList">
                  <el-option
                    :label="item.text"
                    :value="item.id"
                    :key="item.id"
                  />
                </template>
              </el-select>
            </el-col>
            <el-col :span="6">
              <el-select
                v-model="query.payStatus"
                placeholder="支付状态"
                class="order-filter-item"
                clearable
              >
                <template v-for="item in statusLists">
                  <el-option
                    :label="item.text"
                    :value="item.id"
                    :key="item.id"
                  />
                </template>
              </el-select>
            </el-col>
          </el-row>
          <el-row class="out-order-operation">
            <rrOperation :crud="crud" @resetQuery="onResetQuery" />
          </el-row>
          <!-- <el-select
              v-model="query.orderType"
              placeholder="订单类型"
              class="filter-item"
              clearable
            >
              <template v-for="item in orderTypeDataList">
                <el-option
                  :label="item.text"
                  :value="item.id"
                  :key="item.id"
                />
              </template>
          </el-select> -->
        </template>
        <crudOperation/>
      </div>
    </div>
    <div class="my-table">
      <el-table
        v-loading="crud.loading"
        :show-header="false"
        :data="crud.data"
        ref="table"
        >
        <el-table-column label="">
          <template slot-scope="scope">
            <div style="background:#FFFFFF;border:2px #F5F5F5 solid">
              <div class="head-flex" style="margin:5px 16px 5px 16px;border-bottom:1px #F5F5F5 solid;padding:10px 0;">
                <div class="head-flex" style="width:80%">
                  <template v-if="scope.row.orderType == 2">
                    <span class="title-font">{{scope.row.apiOrderNo}}</span>
                  </template>
                  <template v-else>
                    <span class="title-font">{{scope.row.orderNo}}</span>
                  </template>
                  <span class="title-font">{{scope.row.patientName}}</span>
                  <!-- <span class="title-font">{{getPatientSex(scope.row.patientSex)}}</span> -->
                  <!-- <span class="title-font">{{scope.row.patientAge}}</span> -->
                  <span class="title-font">医生姓名：{{scope.row.docName}}</span>
                  <span class="title-font">订单时间：{{scope.row.createTime}}</span>
                </div>
                <div class="head-flex">
                  <span class="title-font">总计：{{parseFloat(scope.row.totalFee).toFixed(2)}} 元</span>
                </div>
              </div>
              <div style="padding-top:0px;margin-top:5px;border-bottom: 0px #f5f5f5 solid;">
                <div v-for="(item , index) in scope.row.infos" :key='index' style="padding: 5px 16px;margin-top:0px;">
                  <div class="head-flex">
                    <div class="list-flex">
                    <span class="option-sort" style="margin-right:10px;width:30px;">{{index + 1}}</span>
                    <span class="content-font paddint-size-list list-width">{{durtType(item.prescriptionType)}}</span>
                    <span class="content-font paddint-size-list list-width-lsit">{{dosageFormType(item)}}</span>
                    <span class="content-font paddint-size-list list-width">¥{{parseFloat(item.totalPay).toFixed(2)}}</span>
                    <div class="paddint-size-list list-width">
                      <template v-if="item.payStatus == 0"><el-tag>{{orderPayStatusType(item.payStatus)}}</el-tag></template>
                      <template v-if="item.payStatus == 1"><el-tag type="success">{{orderPayStatusType(item.payStatus)}}</el-tag></template>
                      <template v-if="item.payStatus == 2"><el-tag type="warning">{{orderPayStatusType(item.payStatus)}}</el-tag></template>
                      <template v-if="item.payStatus == 3"><el-tag type="danger">{{orderPayStatusType(item.payStatus)}}</el-tag></template>
                    </div>
                    <template v-if="item.orderCondition == 1">
                      <!-- 待审方 -->
                      <el-tag class="d-color-1">{{orderStatus(item.orderCondition)}}</el-tag>
                    </template>
                    <template v-else-if="item.orderCondition == 2">
                      <!-- 调剂中 -->
                      <el-tag class="d-color-2">{{orderStatus(item.orderCondition)}}</el-tag>
                    </template>
                    <template v-else-if="item.orderCondition == 3">
                      <!-- 待发货 -->
                      <el-tag type="success">{{orderStatus(item.orderCondition)}}</el-tag>
                    </template>
                    <template v-else-if="item.orderCondition == 4">
                      <!-- 已完成 -->
                      <el-tag class="d-color-4">{{orderStatus(item.orderCondition)}}</el-tag>
                    </template>
                    <template v-else-if="item.orderCondition == 5">
                      <!-- 已作废 -->
                      <el-tag type="danger">{{orderStatus(item.orderCondition)}}</el-tag>
                    </template>
                  </div>
                  <div style="text-align:right">
                    <template v-if="scope.row.orderType == 1">
                      <template v-if="item.payStatus == 0&&item.orderCondition!=5">
                        <span class="option-unable paddint-size" @click="onClickDel(item)">作废</span>
                      </template>
                      <template>
                        <span @click="onClickOrder(scope.row,index)" class="option-btn">{{ btnText(item) }}</span>
                      </template>
                    </template>
                    <!-- <template v-else>
                      <template v-if="item.payStatus == 1 && item.orderCondition != 5">
                        <span class="option-unable paddint-size" @click="onClickDel(item)">作废</span>
                      </template>
                    </template> -->
                  </div>
                  </div>
                </div>
                <div style="border-top:1px #f5f5f5 solid; padding:10px 0 10px 0;margin:5px 16px 0px 16px;">
                  <template v-if="scope.row.orderType == 1">
                    <div v-if="scope.row.payStatus == 0">
                      <div class="head-flex">
                        <div>
                          <!-- <span class="option-btn">{{orderTypeList(scope.row.orderType)}}</span> -->
                        </div>
                        <div style="text-align:right">
                          <span class="option-btn paddint-size" @click="onClickOrder(scope.row,99999)">{{ orderBtnText(scope.row) }}</span>
                          <span class="option-pay" @click="onClickPay(scope.row)">去收费</span> 
                        </div>
                      </div>
                    </div>
                    <div v-else>
                      <div class="head-flex">
                        <div>
                          <span class="content-font paddint-size-list">支付时间：{{scope.row.payTime}}</span>
                          <!-- <span class="option-btn">{{orderTypeList(scope.row.orderType)}}</span> -->
                        </div>
                        <div style="text-align:right">
                          <template v-if="scope.row.payStatus != 4">
                            <span class="option-btn paddint-size" @click="onClickOrder(scope.row,99999)">{{ orderBtnText(scope.row) }}</span>
                          </template>
                          <template v-else>
                            <span class="option-btn" @click="onClickOrder(scope.row,99999)">{{ orderBtnText(scope.row) }}</span>
                          </template>
                          <template v-if="scope.row.payStatus != 4">
                            <span  class="option-unable paddint-size" @click="onRefoundClick(scope.row)" v-if="scope.row.payStatus != 4">退款</span>
                          </template>
                          <template v-else>
                            <span  class="option-unable" @click="onRefoundClick(scope.row)" v-if="scope.row.payStatus != 4">退款</span>
                          </template>
                          <span class="option-print"  @click="onPrintDetails(scope.row)" v-if="scope.row.payStatus != 4">打印</span>
                        </div>
                      </div>
                    </div>
                  </template>
                  <template v-else>
                    <div class="head-flex">
                      <div>
                        <span class="content-font paddint-size">支付时间：{{scope.row.payTime}}</span>
                        <template v-if="scope.row.orderType == 2">
                          <span class="content-font paddint-size-list">{{scope.row.source}} / {{scope.row.sourceStoreName}}</span>
                        </template> 
                        <!-- <span class="option-btn">{{orderTypeList(scope.row.orderType)}}</span> -->
                      </div>
                      <div style="text-align:right">
                        <template v-if="scope.row.payStatus != 4">
                          <span class="option-btn paddint-size" @click="onClickOrder(scope.row,99999)" >{{ orderBtnText(scope.row) }}</span>
                        </template>
                        <template v-else>
                          <span class="option-btn" @click="onClickOrder(scope.row,99999)" >{{ orderBtnText(scope.row) }}</span>
                        </template>
                        <span class="option-print" @click="onPrintDetails(scope.row)" v-if="scope.row.payStatus != 4">打印</span> 
                      </div>
                    </div>
                  </template>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <pagination />
    <!-- 收费明细 -->
    <operateForm 
      ref="operateForm" 
      @refresh="crud.refresh()"
    >
    </operateForm>
    <!-- 打印 -->
    <printSend
      @refresh="onPrintRefresh"
      ref="printSend"
    >
    </printSend>

    <!-- 收费信息 -->
    <el-dialog
      class="clinic-operate-dialog"
      :width="dialogWidths"
      append-to-body
      top="5vh"
      title="收费信息"
      :close-on-click-modal="false"
      :visible.sync="goPayFlage"
      style="cursor:default;"
    >
      <div class="clinic-operate-from">
        <el-row class="pay-content">
          <span style="padding:5px;font-size:14px;">订单金额</span>
          <span style="padding:5px;font-size:14px;">{{payOrder.totalFee}}</span>
        </el-row>
        <el-row class="pay-content">
          <span style="padding:5px;font-size:14px;">订单信息</span>
          <!-- <span style="padding:5px;font-size:14px;">订单编号</span> -->
          <span style="padding:5px;font-size:14px;">{{payOrder.orderNo}}</span>
          <!-- <span style="padding:5px;font-size:14px;">患者姓名</span> -->
          <span style="padding:5px;font-size:14px;">{{payOrder.patientName}}</span>
          <!-- <span style="padding:5px;font-size:14px;">医生姓名</span> -->
          <span style="padding:5px;font-size:14px;">{{payOrder.docName}}</span>
        </el-row>
        <el-row class="pay-content">
          <span>处方信息</span>
          <div v-for="(item,index) in infoDrugs" :key="index" style="padding:5px;">
            <template v-if="item.orderCondition == 5">
            <el-col style="padding:5px;font-size:14px;color:#D3D3D3;" :span="4"><span>{{durtType(item.prescriptionType)}}</span></el-col>
            <el-col style="padding:5px;font-size:14px;color:#D3D3D3;" :span="20"><span>总计:</span>
            <span>{{parseFloat(item.totalPay).toFixed(2)}}</span>
            <span>药材费:</span>
            <span>{{parseFloat(item.drugCost).toFixed(2)}}</span>
            <span v-if="item.processCost">加工费:</span>
            <span v-if="item.processCost">{{parseFloat(item.processCost).toFixed(2)}}</span>
            <span style="font-size:13px;paddint-left:5px;">(已作废)</span>
            </el-col>
            </template>
            <template v-else>
              <el-col style="padding:5px;font-size:14px;" :span="4"><span>{{durtType(item.prescriptionType)}}</span></el-col>
            <el-col style="padding:5px;font-size:14px;" :span="20"><span>总计:</span>
            <span>{{parseFloat(item.totalPay).toFixed(2)}}</span>
            <span>药材费:</span>
            <span>{{parseFloat(item.drugCost).toFixed(2)}}</span>
            <span v-if="item.processCost">加工费:</span>
            <span v-if="item.processCost">{{parseFloat(item.processCost).toFixed(2)}}</span>
            </el-col>
            </template>
            
            
          </div>
        </el-row>
        <el-row class="pay-content">
          <span>取药方式</span>
          <el-radio-group @change="addressTypeChange" v-model="payOrder.deliveryType" style="margin-left:10px;">
          <el-radio :label=2>自取</el-radio>
          <el-radio :label=1>邮寄</el-radio>
          </el-radio-group>
        </el-row>
        <el-row class="pay-content" v-if="payOrder.deliveryType == 1">
          <span>邮寄地址</span>
          <div style="padding:5px;">
          
          <el-col :span="6" style="padding:5px;background:#F5F5F5;">
              <el-input v-model="orderAddress.username" placeholder="收件人"></el-input>
          </el-col>
          <el-col :span="6" style="padding:5px;background:#F5F5F5;">
              <el-input v-model="orderAddress.phone" @input="onInoutCC" maxlength="11" :show-word-limit='false' placeholder="联系电话"></el-input>
            
          </el-col>
          <el-col :span="12" style="padding:5px;background:#F5F5F5;">
              <el-cascader
                    ref="cascader"
                    placeholder="收件人省市区"
                    v-model="cascaderValue"
                    @change="handleChange"
                    style="width: 100%"
                    :options="options"
                    :props="regionProps"
                  >
                  </el-cascader> 
            </el-col>
            <el-col :span="24" style="padding: 0 5px 5px 5px;background:#F5F5F5;">
              <el-input v-model="orderAddress.address" placeholder="详细地址"></el-input>
            
          </el-col>
          </div>
        </el-row>
        <el-row class="pay-content">
          <span style="margin-right:10px">收费优惠</span>
          <el-checkbox @change="shFeeCount" v-model="feeDiscount">减免费用<el-input @input="onInputChange" :disabled='!feeDiscount' v-model="diffFee" placeholder="0" style="width:70px;padding:0 5px 0 5px"></el-input>元</el-checkbox>
          
          <el-checkbox  @change="shFeeCount" v-model="feedrugDiscount">药费折扣<el-input @input="onInputChange" :disabled='!feedrugDiscount' v-model="drugDiscount" placeholder="100" style="width:70px;padding:0 5px 0 5px"></el-input>%</el-checkbox>
        
          <el-checkbox @change="shFeeCount" v-model="feeallDiscount">整体折扣<el-input @input="onInputChange" :disabled='!feeallDiscount' v-model="allDiscount" placeholder="100" style="width:70px;padding:0 5px 0 5px"></el-input>%</el-checkbox>
          <span style="padding-left:10px">(例如:9折,输入90)</span>
        </el-row>
        <el-row class="pay-content">
          <span style="margin-right:10px">其他费用</span>
          <el-input @input="onInputChange" v-model="otherFee" placeholder="0" style="width:70px;padding:0 5px 0 5px"></el-input>
          <span>元</span>
        </el-row>
        <el-row class="pay-content">
          <span style="margin-right:10px">支付方式</span>
          <el-radio-group v-model="payWay">
          <el-radio :label=1>现金</el-radio>
          <el-radio :label=2>微信</el-radio>
          <el-radio :label=3>支付宝</el-radio>
          <el-radio :label=4>银行卡</el-radio>
          <el-radio :label=5>医保</el-radio>
      </el-radio-group>
        </el-row>
        <el-row class="pay-content" v-if="payOrder.deliveryType == 1&&payOrder.drugCost<100">
          <span style="margin-right:10px">邮寄费用</span>
          <span>10</span>
        </el-row>
        <el-row class="pay-content">
          <span style="margin-right:10px">优惠金额</span>
          <span>{{parseFloat(yhFee).toFixed(2)}}</span>
        </el-row>
        <el-row class="pay-content">
          <span style="margin-right:10px">实收金额</span>
          <span>{{parseFloat(sjFee).toFixed(2)}}</span>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <span style="font-size:13px;color:#DC143C;text-align:left;margin-right:30px;">注：如若处方已作废，则不计入订单费用计算</span>
        <template><el-button  type="primary" @click="onClickSumbitPay" :loading="submitLoading">确认</el-button>
        <el-button @click="goPayFlage=false">关闭</el-button>
        </template>
      </div>
    </el-dialog>
    
    <!-- 退款 -->
    <el-dialog
      class="clinic-operate-dialog"
      :width="dialogWidthRefound"
      top="5vh"
      title="退款"
      :close-on-click-modal="false"
      :before-close="refoundClose"
      :visible.sync="refoundFlage"
      :destroy-on-close="true"
    >
      <div class="clinic-operate-from">
        <el-row class="pay-content">
      <el-col :span="2">
          <span>退款入库</span>
      </el-col>
      <el-col :span="22">
      <el-radio-group v-model="refoundSelectMap.type">
        <el-radio :label="0">否</el-radio>
        <el-radio :label="1">是</el-radio>
      </el-radio-group>
      </el-col>
      <div style="margin-top:40px">
      <el-col :span="2">
          <span>退款内容</span>
      </el-col>
      <el-col :span="22">
          <el-radio-group v-model="refoundSelectMap.refundType">
          <el-radio :label="0">部分退款</el-radio>
          <template v-if="refoundMap.payStatus == 3">
              <el-radio :label="1" disabled>退全款</el-radio>
          </template>
          <template v-else>
              <el-radio :label="1">退全款</el-radio>
          </template>
          
          </el-radio-group>
          <div v-for="(item,index) in refoundMap.infos" :key="index" style="padding-top:5px;">
            <el-col style="padding-top:5px;">
            <template v-if="item.payStatus == 3||item.orderCondition == 5">
                <el-checkbox v-model="item.rFlage"  disabled>
                <span style="padding:5px;font-size:14px;">{{durtType(item.prescriptionType)}}</span>
                <span style="padding:5px;font-size:14px;">总计:</span>
                <span style="padding:5px;font-size:14px;">{{parseFloat(item.totalPay).toFixed(2)}}</span>
                <span>药材费:</span>
                <span>{{item.drugCost}}</span>
                <span v-if="item.processCost">加工费:</span>
                <span>{{item.processCost}}</span>
                <span v-if="item.orderCondition == 5" style="font-size:13px;paddint-left:5px;">(已作废)</span>
              </el-checkbox>
            </template>
            <template v-else>
                <el-checkbox :checked="item.rFlage" @change="onChangeFlage(item)">
                <span>{{durtType(item.prescriptionType)}}</span>
                <span>总计:</span>
                <span>{{parseFloat(item.totalPay).toFixed(2)}}</span>
                <span>药材费:</span>
                <span>{{parseFloat(item.drugCost).toFixed(2)}}</span>
                <span v-if="item.processCost">加工费:</span>
                <span v-if="item.processCost">{{parseFloat(item.processCost).toFixed(2)}}</span>
            </el-checkbox>
            </template>
            
            </el-col>
          </div> 
      </el-col>  
      </div>
      <div>
    <template v-if="refoundMap.expressFee > 0">
        <el-col :span="2"><span style="color:#fff">1</span></el-col>
        <el-col :span="22">
          <template v-if="isExpress">
          <el-checkbox v-model="refoundSelectMap.expressFeeFlage" :disabled="isExpress">
                <span>邮寄费用</span>
                <span>{{refoundMap.expressFee}}</span>
              </el-checkbox>
          </template>
          <template v-else>
            <el-checkbox v-model="refoundSelectMap.expressFeeFlage" :disabled="!refoundMap.expressFee">
                <span>邮寄费用</span>
                <span>{{refoundMap.expressFee}}</span>
              </el-checkbox>
          </template>
            
        </el-col>
                
    </template>
      </div>
      <div>
          
    <template v-if="refoundMap.otherFee > 0">
        <el-col :span="2"><span style="color:#fff">1</span></el-col>
        <el-col :span="22">
            <el-checkbox v-model="refoundSelectMap.otherFeeFlage" :disabled="isOther">
                <span>其他费用</span>
                <span>{{refoundMap.otherFee}}</span>
              </el-checkbox>
        </el-col>
                
    </template>
      </div>

        </el-row>
        <div>
        <el-col :span="2">
            <span style="color:#DC143C;font-size:15px;">*</span>
            <span>退款原因</span>
        </el-col>
        <el-col :span="22">
            <el-input v-model="refoundSelectMap.msg"></el-input>
        </el-col>
        
      </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <template><el-button  type="primary" @click="onClickSumitRefund"  :loading="submitLoading">确认</el-button>
        <el-button @click="refoundClose">关闭</el-button>
        </template>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDetails , orderRefundOrder, removeInfo} from '@/api/outpatientService/perscription';
import operateForm from "@/views/charges/clinicCharges/module/operateForm";
import dosageForm from "@/views/charges/clinicCharges/module/dosageForm";
import { getPatientSex, formatMoney } from "@/utils/index";
import { queryLazyTree } from "@/api/charges/logistics";
import { goPayApi} from "@/api/charges/prescription";
import CRUD, { presenter, header } from "@crud/crud";
import crudOperation from '@crud/CRUD.operation';
import printSend from "@/views/charges/print";
import rrOperation from "@crud/RR.operation";
import pagination from "@crud/Pagination";

let defaultCrud = CRUD({
  query: { docName: null, orderCondition: null, orderTime: null, orderType: 1, orderNo: null, 
           patientName: null, payStatus: null, payTime: null },
  hideColumns: ["patientSex","patientAge","phone","totalFee","refund"],
  url: "api/recipel/getPrescriptions",
  queryOnPresenterCreated: false,
  sort: ['create_time,desc'],
  title: "门诊订单"
});

export default {
  name: "clinicCharges",
  mixins: [ presenter(defaultCrud), header() ],
  components: { 
    rrOperation,
    pagination,
    queryLazyTree,
    operateForm,
    dosageForm,
    printSend,
    crudOperation
  },
  computed: {
    listButtonClass() {
      if(this.listSwitchValue === 1) {
        return "active";
      }
      return "";
    },
    cardButtonClass() {
      if(this.listSwitchValue === 0) {
        return "active";
      }
      return "";
    },
    dialogWidths() {
      return window.innerWidth * 0.6 + "px";
    },
    dialogWidthRefound() {
      return window.innerWidth * 0.6 + "px";
    }
  },
  data() {
    return {
      flagList: [
        {id: 0, text: "全部"},
        {id: 1, text: "待审方"},
        {id: 2, text: "调剂中"},
        {id: 5, text: "待发货"},
        {id: 6, text: "已完成"},
        {id: 3, text: "部分退费"},
        {id: 4, text: "已退费"},
      ],
      statusList: [
        {id: 1, text: "待审方"},
        {id: 2, text: "调剂中"},
        {id: 3, text: "待发货"},
        {id: 4, text: "已完成"},
        {id: 5, text: "已发货"},
        {id: 7, text: "已取消"},
      ],
      statusLists: [
        {id: 0, text: "待收费"},
        {id: 1, text: "已收费"},
        {id: 3, text: "部分退款"},
        {id: 4, text: "已退款"},
      ],
      pstTypeList: [
        {id: 0, text: "西药及中成药"},
        {id: 1, text: "饮片"},
        {id: 2, text: "颗粒"},
        {id: 3, text: "打粉"},
        {id: 4, text: "制丸"},
        {id: 5, text: "膏方"},
      ],
      orderTypeDataList: [
        {id: 1, text: "本地订单"},
        {id: 2, text: "云享订单"},
      ],
      listSwitchValue: 1,
      waitNum: 0,
      flag: 1,
      feeDiscount:false,
      feedrugDiscount:false,
      feeallDiscount:false,
      submitLoading: false,
      diffFee:0,
      yhFee:0,
      sjFee:0,
      drugDiscount:100,
      allDiscount:100,
      payWay:1,
      otherFee:0,
      goPayFlage:false,
      refoundFlage:false,
      isExpress:false,
      isOther:false,
      refoundMap:{},
      payOrder:{},
      orderAddress:{},
      options: [],
      cascaderValue: [],
      infoDrugs: [],
      refoundSelectMap:{type:0,infoIds:[],msg:null,orderNo:null,refundType:0,expressFeeFlage:false,otherFeeFlage:false},
      regionProps: {
        lazy: true,
        lazyLoad(node, resolve) {
          let parent = node.value || "00";
          queryLazyTree(node.value || "00").then(res => {
            let data = [];
            let level = node.level;
            res.forEach(item => {
              data.push({
                value: item.value,
                label: item.title,
                leaf: level >= 2
              });
            });
            resolve(data);
          });
        }
      },
    }
  },
  created() {
    this.$store.dispatch("GetInfo").then(res => {
    //   this.query.pharmacyId = res.pharmacyId;
      this.crud.toQuery();
    });
    this._initView();
  },
  mounted() {
    let date = new Date();
    let date2 = new Date(date.getTime() - 7 * 24 * 60 * 60 * 1000);
    if(!this.query.orderTime) {
      this.query.orderTime = [
        this._dateToString(date2),
        this._dateToString(date)
      ];
    }
  },
  methods: {
    getPatientSex, formatMoney,
    [CRUD.HOOK.afterRefresh]() {
      if(this.query.payStatus === 1) {
        this.waitNum = this.crud.page.total;
      } else {
        this.waitNum = 0;
      }
    },
    refoundClose(){
      console.log('----close-------',this.refoundMap);
      var rInfos = this.refoundMap.infos;
      this.refoundMap.infos.forEach(eles => {
        eles.rFlage = false;
      });
      this.refoundFlage = false;
      // this.crud.toQuery();
    },
    addressTypeChange(){
      this.cascaderValue = [];
      this.shFeeCount();
    },
    onInoutCC(){
      this.orderAddress.phone = this.orderAddress.phone.replace(/[^\d]/g, '')
    },
    onClickDel(item){
      const that = this
      this.$confirm('是否确认作废该处方？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        let deleParams = {id:item.id}
        removeInfo(deleParams).then(res => {
          that._success();
        });
      });
    },
    shFeeCount(){
      this.diffFee = this.feeDiscount?this.diffFee:0;
      this.drugDiscount = this.feedrugDiscount?this.drugDiscount:100;
      this.allDiscount = this.feeallDiscount?this.allDiscount:100;
      if(this.payOrder.deliveryType == 1&&this.payOrder.drugCost*parseFloat(this.drugDiscount?this.drugDiscount:100)/100.0*parseFloat(this.allDiscount?this.allDiscount:100)/100.0<100){
      this.sjFee = (parseFloat(this.payOrder.drugCost)*parseFloat(this.drugDiscount?this.drugDiscount:100)/100.0  + parseFloat(this.otherFee?this.otherFee:0) + parseFloat(this.payOrder.machinFee?this.payOrder.machinFee:0))*parseFloat(this.allDiscount?this.allDiscount:100)/100.0 -parseFloat(this.diffFee?this.diffFee:0) + 10.0;
      this.yhFee = parseFloat(this.payOrder.totalFee) + parseFloat(this.otherFee?this.otherFee:0) - parseFloat(this.sjFee) + 10;
      }else{
       this.sjFee = (parseFloat(this.payOrder.drugCost)*parseFloat(this.drugDiscount?this.drugDiscount:100)/100.0 + parseFloat(this.otherFee?this.otherFee:0) + parseFloat(this.payOrder.machinFee?this.payOrder.machinFee:0))*parseFloat(this.allDiscount?this.allDiscount:100)/100.0 -parseFloat(this.diffFee?this.diffFee:0);
       this.yhFee = parseFloat(this.payOrder.totalFee) + parseFloat(this.otherFee?this.otherFee:0) - parseFloat(this.sjFee);
      }
      // this.sjFee = (parseFloat(this.payOrder.drugCost)*parseFloat(this.drugDiscount?this.drugDiscount:100)/100.0 + parseFloat(this.otherFee?this.otherFee:0) + parseFloat(this.payOrder.machinFee?this.payOrder.machinFee:0))*parseFloat(this.allDiscount?this.allDiscount:100)/100.0 -parseFloat(this.diffFee?this.diffFee:0);
      // this.sjFee = (parseFloat(this.payOrder.totalFee) + parseFloat(this.otherFee?this.otherFee:0))*parseFloat(this.allDiscount?this.allDiscount:100)/100.0 - (parseFloat(this.payOrder.drugCost) -(parseFloat(this.payOrder.drugCost)*parseFloat(this.drugDiscount?this.drugDiscount:100))/100.0) - parseFloat(this.diffFee?this.diffFee:0)
      
    },
    onInputChange(){
      this.shFeeCount();
    },
    onClickSumitRefund(){
      if(!this.refoundSelectMap.msg){
        this.$message.error("退款原因不能为空");
        // this.$message.error("退款原因不能为空");
        return;
      }
        if(this.refoundSelectMap.refundType == 1){
          this.refoundSelectMap.infoIds = [];
        }else{
            this.refoundMap.infos.forEach(item => {
                if(item.payStatus != 3&&item.rFlage){
                    this.refoundSelectMap.infoIds.push(item.infoNo)
                }
            });
            if(this.refoundSelectMap.expressFeeFlage){
                this.refoundSelectMap.infoIds.push(1)
            }
            if(this.refoundSelectMap.otherFeeFlage){
                this.refoundSelectMap.infoIds.push(2)
            }
        }

        var paramsData = {};
        paramsData.type = this.refoundSelectMap.type;
        paramsData.infoIds = this.refoundSelectMap.infoIds;
        paramsData.msg = this.refoundSelectMap.msg;
        paramsData.orderNo = this.refoundMap.orderNo;
        paramsData.refundType = this.refoundSelectMap.refundType;
        orderRefundOrder(paramsData).then(res => {
        this.refoundFlage = false;
        this._success();
      });
    },
    // refundexpressFee(){
    //     this.refoundSelectMap.expressFeeFlage = !this.refoundSelectMap.expressFeeFlage;
    // },
    // refundotherFee(){
    //     this.refoundSelectMap.otherFeeFlage = !this.refoundSelectMap.otherFeeFlage;
    // },
    // refundSelect(data){
    //     console.log("dat:::::",data);
    //     if(data.payStatus !=3){
    //         console.log("dat0:::::",data.rFlage);
    //         data.rFlage = !data.rFlage;
    //         console.log("dat1:::::",data.rFlage);
    //     }else{
    //         data.rFlage = true;
    //         console.log("dat2::::",data.rFlage);
    //     }   
    // },
    onClickPay(data) {
        getDetails(data.id).then(res => {
        this.orderAddress={};
        this.cascaderValue = [];
        // this.handleData(res);
        this.payOrder = res.order;
        this.sjFee = this.payOrder.totalFee;
        this.payOrder.deliveryType = this.payOrder.deliveryType?this.payOrder.deliveryType:2;
        this.infoDrugs = this.payOrder.infos;
        this.otherFee = 0;
        this.goPayFlage = true;
      });
        
    },
    onClickSumbitPay(){
      var dataMap = {orderAddress:{}};
      dataMap.allDiscount = this.allDiscount;
      dataMap.deliveryType = this.payOrder.deliveryType;
      dataMap.diffFee = this.diffFee;
      dataMap.drugDiscount = this.drugDiscount;
      dataMap.orderNo = this.payOrder.orderNo;
      dataMap.otherFee = this.otherFee;
      dataMap.payWay = this.payWay;
      if(this.payOrder.deliveryType == 1){
      dataMap.orderAddress.address = this.orderAddress.address;
      dataMap.orderAddress.area = this.orderAddress.area;
      dataMap.orderAddress.areaCode = this.orderAddress.areaCode;
      dataMap.orderAddress.city = this.orderAddress.city;
      dataMap.orderAddress.cityCode = this.orderAddress.cityCode;
      dataMap.orderAddress.orderNo = this.orderAddress.orderNo;
      dataMap.orderAddress.phone = this.orderAddress.phone;
      dataMap.orderAddress.province = this.orderAddress.province;
      dataMap.orderAddress.provinceCode = this.orderAddress.provinceCode;
      dataMap.orderAddress.username = this.orderAddress.username;
      if(!this.orderAddress.address||!this.orderAddress.area||!this.orderAddress.city||!this.orderAddress.province||!this.orderAddress.username||!this.orderAddress.phone){
        this.$message.error("邮寄地址不能为空");
        return;
      }
      }
      this.submitLoading = true;
      goPayApi(dataMap).then(() => {
        this.goPayFlage = false;
        this.submitLoading = false;
        this._success();
      })
    },
    onClickOrder(row,index) {
      this.$refs.operateForm.open(row, index,1);
    },
    _initView() {
      this._loadLazyTree({
        children: this.options,
        parent: "00"
      });
    },
    _loadLazyTree({ parent, children}) {
      queryLazyTree(parent).then(res => {
        res.forEach(item => {
          children.push({
            value: item.value,
            label: item.title
          });
        });
      });
    },
    handleChange() {
      let nodes = this.$refs.cascader.getCheckedNodes()[0].pathLabels;
      this.orderAddress.province = nodes[0];
      this.orderAddress.city = nodes[1];
      this.orderAddress.area = nodes[2];

      this.orderAddress.provinceCode = this.cascaderValue[0];
      this.orderAddress.cityCode = this.cascaderValue[1];
      this.orderAddress.areaCode = this.cascaderValue[2];


      this.orderAddress.orderNo = this.payOrder.orderNo;
    },
    dosageFormType(item){
        if(item.pstType == 1){
         switch (item.dosageForm) {
             case 1:
                 return '自煎'
                 break;
            case 2:
                 return '代煎'
                 break;
             default:
                 return '/';
                 break;
         }
        }else{
            return '/';
        }
    },
    durtType(type){
        switch (type) {
            case 1:
                return '中药处方(饮片)'
                break;
            case 2:
                return '成药处方'
                break;
            case 3:
                return '中药处方(颗粒剂) '
                break;
            case 4:
                return '输液处方'
                break;
            case 5:
                return '敷贴处方'
                break;
            default:
                break;
        }
    },
    orderPayStatusType(type) {
      switch (type) {
        case 0:
            return '未支付'
            break;
        case 1:
            return '已支付'
            break;
        case 2:
            return '部分退款'
            break;
        case 3:
            return '已退款'
            break;
        default:
            return '/'
            break;
      }
    },
    orderStatus(type){
        switch (type) {
            case '1':
                return '待审方'
                break;
            case '2':
                return '调剂中'
                break;
            case '3':
                return '待发货'
                break;
            case '4':
                return '已完成'
                break;
            case '5':
                return '已作废'
                break;
            default:
              return '/'
                break;
        }
    },
    onChangeFlage(item){
        item.rFlage = !item.rFlage;
    },
    onPrintDetails(row) {
      this.$refs.printSend.open(row.id);
    },
    onRefoundClick(row) {
        getDetails(row.id).then(res => {
        this.handleData(res);
      });
        
    },
    handleData(data) {
      this.refoundMap = {};
      this.refoundSelectMap={type:0,infoIds:[],msg:null,orderNo:null,refundType:0,expressFeeFlage:false,otherFeeFlage:false};
      this.refoundMap = data.order;

      if(this.refoundMap.payStatus == 3 ||this.refoundMap.payStatus == 4 ){
      this.refoundSelectMap.expressFeeFlage = data.refundExp;
      this.refoundSelectMap.otherFeeFlage = data.refundOtherFee;
      this.refoundSelectMap.msg = data.msg;
      if(data.refundExp){
          this.isExpress = true;
      }
      if(data.refundOtherFee){
          this.isOther = true;
      }
      }
      this.refoundMap.infos.forEach(item => {
          if(item.payStatus != 3){
              item.rFlage = false; //未退款
          }else{
              item.rFlage = true;
          }
      });
      console.log("refoundMap.infos:::",this.refoundMap.infos);
      
      this.refoundFlage = true;
    },
    onOperateRow(operate, id) {
      switch(operate) {
        case 6:
          // 打印
          this.$refs.printSend.open(id);
          break;
        default:
          this.$refs.operateForm.open(operate, id,1);
          break;
      }
    },
    onResetQuery() {
      this.defaultDate = null;
    },
    onChangeFlag() {
      switch(this.flag) {
        case 0:
          this.query.payStatus = null;
          break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
          this.query.payStatus = this.flag;
          break;

      }
      this.crud.toQuery();
    },
    onRowHandle(btn) {
      btn.handle.apply(this, btn.params)
    },
    onCheckOrder(orderNo) {
      let payStatus = 2;
      checkOrder({ orderNo, payStatus }).then(() => {
        this._success();
      })
    },
    onConfirmOrder(orderNo) {
      this.$confirm("确认调剂？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        confirmOrder(orderNo.id).then(() => {
          this._success();
        })
      }).catch(() => {});
    },
    // 根据处方状态确定按钮文字
    btnText(row) {
      let { orderCondition, payStatus } = row;
      let res = "点击查看";
      if(payStatus === 1 || payStatus === 2) {
        switch(orderCondition) {
          case "1":
            res = "审方";
            break;
          case "2":
            res = "调剂";
            break;
        }
      }
      return res;
    },
    // 打印页操作回调
    onPrintRefresh() {
      let operateForm = this.$refs.operateForm;
      this.crud.refresh();
      if(operateForm.visible) {
        operateForm.refresh(null, false);
      }
    },
    // 外层，订单查看按钮文字
    orderBtnText(row) {
      let { orderCondition, payStatus, orderType } = row;
      let res = "查看订单";

      if(payStatus === 1 || (orderType === 2 && payStatus === 3)) {
        switch(orderCondition) {
          case 1:
            res = "审方";
            break;
          case 2:
            res = "调剂";
            break;
        }
      }
      return res;
    },
    _success() {
      this.$notify({
        title: "操作成功",
        type: "success"
      });
      this.crud.refresh();
    },
    _dateToString(date) {
      return date.getFullYear() + "-" + (date.getMonth()> 8 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "-" +(date.getDate() > 8 ? (date.getDate()) : "0" + (date.getDate()));
    },
  }
}
</script>

<style src="./style/index.css">
.optionbtn{
  padding-left: 10px;
  color: cornflowerblue;
  font-size: 15px;
}
</style>